//
//  JXUserInfoCtl.m
//  JiaoX
//
//  Created by yinqy on 15/4/8.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXUserInfoCtl.h"
#import "ForumDataTool.h"
#import "PostsItemFrame.h"
#import "PostsItemCell.h"
#import "MinePostsHeaderView.h"
#import "JXPostsDetailCtl.h"
#import "UserInfoHeaderView.h"

@interface JXUserInfoCtl ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,copy) NSString *userId;
@property (nonatomic,assign) NSInteger currentPage;
@property (nonatomic,strong) UserInfoHeaderView *headerView;
@property (nonatomic,strong)NSMutableArray *itemFrameArray;
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) UIView *nullView;

@end

@implementation JXUserInfoCtl


-(instancetype)initWithUserId:(NSString *)userId{
    self = [super init];
    if (self) {
        self.userId = userId;
        self.currentPage = 1;
        self.headerView = [[UserInfoHeaderView alloc]init];
        self.itemFrameArray = [[NSMutableArray alloc]init];
        self.nullView = [self getNullView];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:JXGlobalColorBg];
    [self setTitle:@"用户信息"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"举报" style:UIBarButtonItemStyleBordered target:self action:@selector(report)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    [self setUpView];
    [self requestData];
}

-(void)setUpView{
    [self.headerView setY:NavHeight];
    [self.view addSubview:self.headerView];
    [self.headerView.userInfoView setType:1];
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerView.frame), JXScreenW, JXScreenH-CGRectGetMaxY(self.headerView.frame))];
    [tableView setDataSource:self];
    [tableView setDelegate:self];
    [tableView setTableFooterView:[UIView new]];
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

-(void)requestData{
    [ForumDataTool getForumUserInfoWithUserId:self.userId pageIndex:self.currentPage success:^(PersonInfo *result) {
        if (ISBLANK(result.data)) {
            if (self.currentPage == 1) {
                [_itemFrameArray removeAllObjects];
                [self.headerView.userInfoView setUserInfo:result.userInfo];
            }
            [self convertDataToFrame:result.postsList];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:self.navigationController.view String:@"加载失败"];
    }];
}

-(void)convertDataToFrame:(NSArray *)postsList{
    if (postsList!=nil &&postsList.count>0) {
        self.currentPage ++;
        [self.nullView removeFromSuperview];
        [self.tableView setHidden:NO];
    }else{
        if (self.currentPage==1) {
            [self.nullView setCenterX:self.tableView.centerX];
            [self.nullView setCenterY:self.tableView.centerY];
            [self.view addSubview:self.nullView];
            [self.tableView setHidden:YES];
        }
    }
    for (BBSPosts *post in postsList) {
        PostsItemFrame *frame = [[PostsItemFrame alloc]init];
        [frame setPosts:post];
        [_itemFrameArray addObject:frame];
    }
    [self.tableView reloadData];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _itemFrameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PostsItemCell *cell = [PostsItemCell cellWithTableView:tableView];
    [cell setPostsItemFrame:_itemFrameArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    PostsItemFrame *frame = _itemFrameArray[indexPath.row];
    return frame.frame.size.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PostsItemFrame *frame = _itemFrameArray[indexPath.row];
    JXPostsDetailCtl *ctl = [[JXPostsDetailCtl alloc]initWithBBSPosts:frame.posts];
    [[[self.view superview] viewController].navigationController pushViewController:ctl animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 私有方法
-(void)report{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"言论涉及色情、暴力、广告等方可举报\n举报后对方将被禁言，请勿随便举报" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [ForumDataTool reportUserWithUserId:self.userId success:^(JXBaseResponse *result) {
            if ([Utils isBlankString:result.data]) {
                [DialogUtils showTipsWithView:self.navigationController.view String:@"举报成功"];
            }else{
                [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
            }
        } failure:^(NSError *error) {
            [DialogUtils showTipsWithView:self.navigationController.view String:@"举报失败"];
        }];
    }
}

-(UIView *)getNullView{
    UIView *view = [[UIView alloc]init];
    UIImageView *image = [[UIImageView alloc]initWithImage:IMG(@"ic_user_empty_post")];
    UILabel *label = [[UILabel alloc]init];
    [label setText:@"TA还没有发布过帖子哦"];
    [label setTextColor:JXColor(102, 102, 102)];
    [label setFont:SYS_FONT(SCALEX(12))];
    [label setY:CGRectGetMaxY(image.frame)+10];
    [label setSize:[label.text sizeWithFont:label.font]];
    
    [image setCenterX:label.centerX];
    [view addSubview:image];
    [view addSubview:label];
    [view setWidth:label.width];
    [view setHeight:CGRectGetMaxY(label.frame)];
    return view;
}

@end
